from django.shortcuts import render
from django.http import JsonResponse
from api import models


# Create your views here.
def fetch_task(request, phone):
    # 1。接收传入的手机号phone
    # 2.去数据库查询
    #   2。1传入的手机号小于11位，则返回：{'status': False, 'data': '手机号错误'}
    if len(phone) < 11:
        return JsonResponse({'status': False, 'data': '手机号错误'})
    #   2.2如果数据库中没有数据，则返回结果
    if not models.INFO.objects.filter(phone=phone):
        return JsonResponse({'status': False, 'data': '请先录入数据'})

    if models.INFO.objects.filter(phone=phone):
        ph = models.INFO.objects.filter(phone=phone)
        print(ph)
        return JsonResponse(ph.values('name','token', 'data')[0])
